package com.company.cloud.acct;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.Collections;

//@Intercepts({@Signature(type = Executor.class, method = "insert", args = {MappedStatement.class, Object.class})})
//@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class MyInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        System.out.println("+++++++ MyInterceptor    1");
//        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
//        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
//        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//        mappedStatement = buildAutoCountMappedStatement(mappedStatement);
//        metaObject.setValue("delegate.mappedStatement",mappedStatement2);



//         Object[] args = invocation.getArgs();
//        MappedStatement statement = (MappedStatement) args[0];

        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler,
                SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
                new DefaultReflectorFactory());

         MappedStatement mappedStatement = (MappedStatement)
                 metaObject.getValue("delegate.mappedStatement");


        Field field = mappedStatement.getClass().getDeclaredField("resultSetType");
        field.setAccessible(true);
        field.set(mappedStatement,ResultSetType.SCROLL_INSENSITIVE);

//        ResultSet.TYPE_FORWARD_ONLY




        System.out.println("+++++++ MyInterceptor   2");
        return invocation.proceed();




    }


    private MappedStatement buildAutoCountMappedStatement(MappedStatement ms) {
        final Configuration configuration = ms.getConfiguration();
        MappedStatement.Builder builder = new MappedStatement.Builder(
                configuration, ms.getId(), ms.getSqlSource(), ms.getSqlCommandType());

        builder.resource(ms.getResource());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());

        builder.fetchSize(ms.getFetchSize());

        builder.statementType(ms.getStatementType());
        builder.timeout(ms.getTimeout());

//        builder.resultSetType(ms.getResultSetType());
        builder.resultSetType(ResultSetType.SCROLL_INSENSITIVE);
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        builder.resultOrdered(ms.isResultOrdered());
        builder.keyGenerator(ms.getKeyGenerator());
//        builder.keyProperty(ms.getKeyProperties())
//        builder.keyColumn(ms.getKeyColumns())
        builder.databaseId(ms.getDatabaseId());
        builder.lang(ms.getLang());

//        builder.resultSets(ms.getResultSets())






        return builder.build();
    }


}
